package controllers;

import java.io.File;
import java.io.IOException;

import models.Recipe;

import org.apache.commons.io.FileUtils;

import play.Logger;
import play.Play;
import play.modules.siena.SienaFixtures;
import play.mvc.Controller;
import play.mvc.With;

import com.google.appengine.api.datastore.Blob;

/**
 * @author oleksandr.bezhan oleksandr.bezhan@d8corp.com
 * 
 *         Apr 9, 2012
 */
@With(Secure.class)
public class BootstrapController extends Controller {

    public static void deleteDB() {
        Logger.info("Delete DB is requested...");
        String result = null;
        try {
            SienaFixtures.deleteDatabase();
        } catch (RuntimeException e) {
            result = "Exception occured while deleting the DB: " + e.getMessage();
        }
        result = "DB is deleted.";
        renderText(result);
    }

    public static void initDB() {
        Logger.info("Bootstrap: Play ID: " + Play.id);

        String result = null;
        if (Recipe.all().count() == 0) {
            Logger.info("Creating the database using init-data.yml");
            SienaFixtures.loadModels("init-data.yml");
            result = "Initialized.";
        } else {
            result = "Recipes already exist in DB.";
        }
        renderText(result);
    }

    private static void initRecipes() {
        saveRecipeImages("kartofel-zharenyj", "kartofel.jpg", "kartofel-thumb.jpg");
        saveRecipeImages("kotlety", "kotlets.jpg", "kotlets-thumb.jpg");
        saveRecipeImages("salat-cezar", "salad-cezar.jpg", "salad-cezar-thumb.jpg");
        saveRecipeImages("salat-olive", "salad-olivie.jpg", "salad-olivie-thumb.jpg");
        saveRecipeImages("yaichnica-glazunya-s-kolbasoj-i-pomidorami",
                        "yaichnica-glazunya-s-kolbasoj-i-pomidorami.jpg",
                        "yaichnica-glazunya-s-kolbasoj-i-pomidorami.jpg");
        saveRecipeImages("borsch-ukrainskij", "borshch.jpg", "borshch-thumb.jpg", "borshch2.jpg", "borshch2-thumb.jpg");
        saveRecipeImages("kolbasa-zharenaya-po-domashnemu", "kolbasa-zharenaya-po-domashnemu.jpg", "kolbasa-zharenaya-po-domashnemu-thumb.jpg");
        saveRecipeImages("salat-bukovina", "salat-bukovina.jpg", "salat-bukovina-thumb.jpg");
        saveRecipeImages("okroshka-domashnyaya", "okroshka-domashnyaya.jpg", "okroshka-domashnyaya-thumb.jpg");
        // try {
        // File imagesFile = new File("conf/recipe-images");
        // BufferedReader reader = new BufferedReader(new
        // FileReader(imagesFile));
        // String line = reader.readLine();
        // String[] args = line.split(":");
        // saveRecipeImages(args[0], args[1], args[1]);
        // reader.close();
        // } catch (IOException e) {
        // e.printStackTrace();
        // }
    }

    public static void reinitDB() {
        String result = null;
        // try {
        SienaFixtures.deleteDatabase();

        if (Recipe.all().count() == 0) {
            Logger.info("Creating the database using init-data.yml");
            SienaFixtures.loadModels("init-data.yml");
            initRecipes();
            result = "Initialized.";
        } else {
            result = "Recipes already exist in DB.";
        }
        Controller.response.status = 200;
        // } catch (RuntimeException e) {
        // result = "Exception occured while deleting the DB: " +
        // e.getMessage();
        // Controller.response.status = 500;
        // }
        renderText(result);
    }

    private static void saveRecipeImages(String recipeCode, String image, String thumb) {
        saveRecipeImages(recipeCode, image, thumb, null, null);
    }

    private static void saveRecipeImages(String recipeCode, String image, String thumb, String image2, String thumb2) {
        try {
            Recipe recipe = Recipe.all().filter("code", recipeCode).get();
            File file = null;
            byte[] bytes;

            file = new File(Play.applicationPath + "/public/images/" + image);
            bytes = FileUtils.readFileToByteArray(file);
            recipe.image = new Blob(bytes);

            file = new File(Play.applicationPath + "/public/images/" + thumb);
            bytes = FileUtils.readFileToByteArray(file);
            recipe.thumb = new Blob(bytes);

            if (image2 != null) {
                file = new File(Play.applicationPath + "/public/images/" + image2);
                bytes = FileUtils.readFileToByteArray(file);
                recipe.image2 = new Blob(bytes);
            }

            if (thumb2 != null) {
                file = new File(Play.applicationPath + "/public/images/" + thumb2);
                bytes = FileUtils.readFileToByteArray(file);
                recipe.thumb2 = new Blob(bytes);
            }

            recipe.save();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
